package com.bbg.amyam.web.util.view;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;

import com.bbg.amyam.web.util.menu.MenuItem;

public abstract class AbstractGenerator {

	protected final static String C_SRC = "src";
	protected final static String C_MAIN = "main";
	protected final static String C_WEBAPP = "webapp";
	protected final static String C_WEB_INF = "WEB-INF";
	protected final static String C_I18N = "i18n";
	protected final static String C_VIEWS = "views";
	protected final static String C_VIEWS_XML = "views.xml";
	protected final static String C_MODULE_TEMPLATE = "module-template";

	protected final static String C_MODULE_JAVA = "java";
	protected final static String C_MODULE_COM = "com";
	protected final static String C_MODULE_BBG = "bbg";
	protected final static String C_MODULE_AMYAM = "amyam";
	protected final static String C_MODULE_WEB = "web";
	protected final static String C_MODULE_MVC = "mvc";
	protected final static String C_MODULE_CONTROLLER = "controller";

	protected final static String C_MODULE_MODULE_TEMPLATE = "module-template";
	protected final static String C_MODULE_CONTROLLER_JAVA = "Controller.java";
	
	protected final static String C_MODULE_VIEW = "view";
	
	

	protected MenuItem menuItem;
	protected boolean overwrite;
	protected HashMap<String, String> replacedValues = new HashMap<String, String>();

	public AbstractGenerator(MenuItem item, boolean overwrite) {
		this.menuItem = item;
		this.overwrite = overwrite;

	}

	public String getControllerDirPath() {
		return C_SRC + File.separator + C_MAIN + File.separator + C_MODULE_JAVA
				+ File.separator + C_MODULE_COM + File.separator + C_MODULE_BBG
				+ File.separator + C_MODULE_AMYAM + File.separator
				+ C_MODULE_WEB + File.separator + C_MODULE_MVC + File.separator
				+ C_MODULE_CONTROLLER + File.separator+"views"+File.separator;
	}
	
	public String getWebflowServicerDirPath() {
		return C_SRC + File.separator + C_MAIN + File.separator + C_MODULE_JAVA
				+ File.separator + C_MODULE_COM + File.separator + C_MODULE_BBG
				+ File.separator + C_MODULE_AMYAM + File.separator
				+ C_MODULE_WEB + File.separator + "flow" + File.separator
				+ "service" + File.separator;
	}

	public String getControllerTemplateFile() {
		if(this.menuItem.getControllertemplate()!=null){
			return this.getModuleTemplateDIr() + C_MODULE_CONTROLLER
					+ File.separator +this.menuItem.getControllertemplate()+".JAVA";
		}
		return this.getModuleTemplateDIr() + C_MODULE_CONTROLLER
				+ File.separator + C_MODULE_CONTROLLER_JAVA;
	}
	
	public String getWebflowServiceTemplateFile() {
		return C_SRC + File.separator + C_MAIN + File.separator + C_WEBAPP
				+ File.separator + C_MODULE_MODULE_TEMPLATE + File.separator+"webflowservice"+File.separator;
	}

	private String getModuleTemplateDIr() {
		return C_SRC + File.separator + C_MAIN + File.separator + C_WEBAPP
				+ File.separator + C_MODULE_MODULE_TEMPLATE + File.separator;

	}
	
	public String getDomainClassPackagePath() {
		return C_SRC + File.separator + C_MAIN + File.separator + C_MODULE_JAVA
				+ File.separator + C_MODULE_COM + File.separator + C_MODULE_BBG
				+ File.separator + C_MODULE_AMYAM + File.separator
				+ "domain" + File.separator + "model" + File.separator;
	}
	
	public String getRequestPaserClassPath() {
		return C_SRC + File.separator + C_MAIN + File.separator + C_MODULE_JAVA
				+ File.separator + C_MODULE_COM + File.separator + C_MODULE_BBG
				+ File.separator + C_MODULE_AMYAM + File.separator
				+ "web" + File.separator + "util" + File.separator+"RequestPathPaser.java";
	}
	

	public abstract void execute() throws IOException;

	public String getViewDirPath() {
		return C_SRC + File.separator + C_MAIN + File.separator + C_WEBAPP
				+ File.separator + C_WEB_INF + File.separator + C_VIEWS
				+ File.separator;
	}

	public String getViewTemplateDir() {
		return this.getModuleTemplateDIr() + "view"+File.separator;
	}
	
	protected String processLine(String line) {
		for (Entry<String, String> entry : this.replacedValues.entrySet()) {
			line = StringUtils.replace(line, entry.getKey(), entry.getValue());
		}
		return line;
	}

	public String getAmyamMsgFileName() {
		return  C_SRC + File.separator + C_MAIN + File.separator
				+ C_WEBAPP + File.separator + C_WEB_INF + File.separator
				+ C_I18N + File.separator + "amyam.properties";
	}
}
